package com.spm.librarianmanagersystem.controller;

import com.alibaba.fastjson.JSONObject;
import com.spm.librarianmanagersystem.service.ReturnService;
import com.spm.librarianmanagersystem.utils.JsonResult;
import com.spm.librarianmanagersystem.utils.UserContextHolder;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class ReturnController {
    @Autowired
    ReturnService service;

    @PostMapping("/api/borrowing/return")
    public JsonResult<String> returnBook(@RequestBody JSONObject jsonObject){
        String currentUserId = UserContextHolder.getCurrentUserId();
        try {
            String msg = service.returnBook(currentUserId, jsonObject.getInteger("borrowing_id"));
            return new JsonResult<>(0, msg);
        } catch (Exception e) {
            return new JsonResult<>(1, e.getMessage());
        }
    }
}
